<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\daikuan\controller;

use cmf\controller\AdminBaseController;
use app\user\model\UserModel;
use app\daikuan\model\LogisticsModel;

/**
 * Description of MyLogistcsController
 *
 * @author PVer
 */
class MyLogisticsController extends AdminBaseController {

    public function renzheng() {
        $id = cmf_get_current_admin_id();
        $map = array();

        $map["a.id"] = $id;
        $userModel = new UserModel();
        $info = db('user a ')->join(array(array('dk_logistics b', 'a.id=b.UserId', 'LEFT')
                ))
                ->field(' a.user_nickname,a.avatar,a.birthday,a.create_time,a.id,a.logistics,a.mobile,b.LogisticsName,b.* ,b.BusLis Img1')
                ->where($map)
                ->find();
        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    //put your code here
    public function index() {
        $id = cmf_get_current_admin_id();
        $map = array();
//        table排序
        $order = array();
        $order_type = input('order') ?? 'asc';
        $order_name = input('order_name') ?? 'b.CanLoanAmount';
        if (empty($order_name)) {
            $order_name = 'b.CanLoanAmount';
        }
        $order = "$order_name $order_type";

        $map["a.user_category"] = 2;
        //$map["a.cutomerid"] = $id;
        $dk_status = input('dk_status', -1, 'intval');
        if ($dk_status >= 0) {
            $map['a.dk_status'] = $dk_status;
        }
        $name = input('name');
        if (!empty($name)) {
            $map['b.Legal|b.LegalPhone|b.LogisticsName|b.Id'] = ['like', '%' . $name . '%'];
        }

        $list = db('user a ')->join(array(array('dk_logistics b', 'a.id=b.UserId', 'LEFT')
                ))
                ->field('a.id,a.user_nickname,b.*')
                ->where($condition)
                ->where("a.id in (select DISTINCT(logid) from dk_customer_logistics where customerid=$id)")
                ->where($map)
                ->order($order)
                ->paginate(10, false, ['query' => request()->param(),]);
        $page = $list->render();
        $this->assign('order', $order_type);
        $this->assign('dk_status', $dk_status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function myindex() {
        $id = cmf_get_current_admin_id();
        //        table排序
        $order = array();
        $order_type = input('order') ?? 'desc';
        $order_name = input('order_name') ?? 'a.id';
        if (empty($order_name)) {
            $order_name = 'a.id';
        }
        $order = "$order_name $order_type";
        $status = input('status', -1, 'intval');
        if ($status >= 0) {
            $map['a.Status'] = $status;
        }
        $name = input('name');
        if (!empty($name)) {
            $map['b.user_nickname'] = array('like', "%$name%");
        }

        $list = db('logistics_loan a')->join(array(array('dk_user b', 'a.LogisticsId=b.id ', 'LEFT')
                    , array('dk_logistics c', 'b.id=c.UserId', 'LEFT')
                ))
                ->field('a.*,b.user_nickname,c.LogisticsType,c.RegistAmount,c.Legal,c.CreateDate LogCreateDate,c.WorkDays,c.LogisticsName,c.CreateDate')
                ->where($map)
                ->order($order)
                ->where("a.CustomerId=$id")
                ->paginate(10, false, ['query' => request()->param(),]);
        //echo db('logistics_loan a')->getLastSql();
        $page = $list->render();
        $this->assign('order', $order_type);
        $this->assign('Status', $status);
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function queren() {
        $id = input('id');

        $info = db('logistics_loan a')->join(array(array('dk_user b', 'a.LogisticsId=b.id ', 'LEFT')
                    , array('dk_logistics c', 'b.id=c.UserId', 'LEFT')
                ))
                ->field('a.*,b.user_nickname,c.LogisticsType,c.RegistAmount,c.Legal,c.CreateDate LogCreateDate,c.WorkDays,c.LogisticsName,c.CreateDate')
                ->where("a.Id=" . $id)
                ->find();


        if (empty($info)) {
            $this->error("数据错误");
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function editPost() {
        if ($this->request->isPost()) {
            $id = input('Id');
            $data = input('post.');
            $result = false;
            if (isset($data['audit'])) {
                $arr = array();
                $arr["Status"] = 5;
                $result = model('LogisticsLoan')->save($arr, ['Id' => $id]);
            } elseif (isset($data['refuse'])) {
                $arr = array();
                $arr["Status"] = 6;
                $result = model('LogisticsLoan')->save($arr, ['Id' => $id]);
            }
            if ($result !== false) {
                $this->success("操作成功", url("myindex"));
            } else {
                $this->error("操作失败");
            }
        }
    }

    public function addrenzhengPost() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $id = $data['id'];
            $userModel = new UserModel();
            $user['user_nickname'] = $data['user_nickname'];
            $user['mobile'] = $data['LegalPhone'];
            $user['user_category'] = 2;
            $user['user_type'] = 3;
            // $user['dk_status'] = 0;

            $result_1 = $userModel->save($user, 'id=' . $id);
            if ($result_1 !== false) {
                $logisticsModel = new LogisticsModel();
                $arr1 = array();
                $arr1['UserId'] = $id;
                $arr1['LogisticsName'] = $data['user_nickname'];
                $arr1['LogisticsType'] = $data['LogisticsType'];
                $arr1['Legal'] = $data['Legal'];
                $arr1['LegalPhone'] = $data['LegalPhone'];
                $arr1['RegistAmount'] = $data['RegistAmount'];
                $arr1['CreateDate'] = $data['CreateDate'];
                $arr1['WorkDays'] = $data['WorkDays'];
                $arr1['BusLis'] = $data['imgid1'];
                $res1 = $logisticsModel->where('UserId=' . $id)->select();
                if (count($res1) > 0) {
                    $result_2 = $logisticsModel->save($arr1, 'UserId=' . $id);
                } else {
                    $result_2 = $logisticsModel->save($arr1);
                }
                if ($result_1 !== false && $result_2 !== false) {
                    $this->success("操作成功", "renzheng");
                } else {
                    $this->error("操作失败");
                }
            } else {
                $this->error("请求错误1");
            }
        } else {
            $this->error("请求错误");
            return $this->fetch(':index');
        }
    }

}
